/**
 * 
 */
package com.sqltojson.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sqltojson.core.Query;

/**
 *
 */
public final class Util {
	
	/** Le logger */
	private static final Logger LOGGER = LoggerFactory
			.getLogger(Util.class);
	
	/** chargement des properties*/
	public static final Properties PROP = loadProp();
	
	/** LA source jndi*/
	public static final String DB_JNDI_NAME = "db.jndi.name";
	/** Le dossier contenant les requetes */
	private static final String BEGIN = "/com/sqltojson/";
	/** le fichier doit se terminer ar .xml */
	private static final String END = ".xml";

	/**
	 * 
	 */
	private Util() {
	}
	
	private static Properties loadProp() {
		final Properties pro = new Properties();
		try {
			pro.loadFromXML(getResourceAsStream("sqltojsonproperties"));
		} catch (InvalidPropertiesFormatException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return pro;
	}

	public static InputStream getResourceAsStream(final String name) {
		final StringBuilder fileName = generateBegin(name);
		fileName.append(END);
		LOGGER.info("fichier {}", fileName);
		final InputStream resourceAsStream = Query.class
				.getResourceAsStream(fileName.toString());
		return resourceAsStream;
	}
	
	private static StringBuilder generateBegin(final String name) {
		final StringBuilder fileName = new StringBuilder();
		fileName.append(BEGIN);
		fileName.append(name);
		return fileName;
	}

}
